package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.nio.file.Watchable;
import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    /**
     * 按照条件分页查询频道列表，按照顺序编号升序排序
     * @param channelDTO
     * @return
     */
    @Override
    public ResponseResult findByNameAndPage(ChannelDTO channelDTO) {
        //1.参数校验
        if (channelDTO == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        channelDTO.checkParam();
        //2.分页查询
        //2.1封装查询的条件
        Page<AdChannel> adChannelPage = new Page<>(channelDTO.getPage(), channelDTO.getSize());
        //3.其他查询
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.lambdaQuery();
        //3.1查询频道名字
        if (StringUtils.isNotBlank(channelDTO.getName())){
            queryWrapper.like(AdChannel::getName,channelDTO.getName());
        }
        //3.2查询状态
       if (channelDTO.getStatus() !=null){
           queryWrapper.eq(AdChannel::getName,channelDTO.getStatus());
       }
        //4.执行查询的结果
        //升序排序
        queryWrapper.orderByAsc(AdChannel::getOrd);
        //5.执行查询
        IPage<AdChannel> pageresult = this.page(adChannelPage, queryWrapper);
        //6.封装响应的结果
        PageResponseResult pageResponseResult = new PageResponseResult(channelDTO.getPage(),
                channelDTO.getSize(),
                pageresult.getTotal(),
                pageresult.getRecords());

        return pageResponseResult;
    }

    /**
     * 添加频道
     * @param channel
     * @return
     */
    @Override
    public ResponseResult insertnews(AdChannel channel) {
        //1.参数校验
        //2.频道名称不能为空
       if (StringUtils.isBlank(channel.getName())){
           return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能为空");
       }
//        //3.频道名称不能超过10
//        if (channel.getName().length()>10){
//            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称长度不能超过10");
//        }
        //4.频道名称不能重复
        LambdaQueryWrapper<AdChannel> eq = Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, channel.getName());
        int count = this.count(eq);
        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能重复");
        }
        //新增时间
        channel.setCreatedTime(new Date());
        save(channel);
        return ResponseResult.okResult();
    }

    /**
     * 频道信息修改
     * @param channel
     * @return
     */
    @Override
    public ResponseResult updatenews(AdChannel channel) {
        //1.参数校验
        if (channel.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"id为空");
        }
        //2.执行修改
        AdChannel oldChannel = this.getById(channel);
        if (oldChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"id不存在");
        }
        //3.判断频道不能为空并且频道不能重复
        //获取频道名称
        String name = channel.getName();
        if (StringUtils.isNotBlank(name) && !name.equals(oldChannel.getName())){
            //判断频道长度是否大于10
            if (channel.getName().length()>10){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道长度不能大于10");
            }
            if (this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName,channel.getName()))>0){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道不能重复");
            }
        }
        //执行修改
        updateById(channel);
        //返回结果
        return ResponseResult.okResult();
    }

    /**
     *频道删除，被启动的频道不能被删除
     * @param id
     * @return
     */
    @Override
    public ResponseResult deletenews(Integer id) {
        //1.参数校验
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"参数为空");
        }
        //2.判断频道id是否存在
        //获取频道id
        AdChannel channel = this.getById(id);
        if (channel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道id不存在");
        }
        //3.判断频道是否启动
        if (channel.getStatus()==false){
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_STATUS_TRUE,"频道无法删除");
        }
        //4.执行删除方法
        removeById(id);
        //int i=1/0;
        //5.返回结果
        return ResponseResult.okResult();
    }
}
